package com.itheima.exception;

import com.itheima.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice// 全局异常处理
public class GlobalExceptionHandler {
    @ExceptionHandler// 异常处理方法
    public Result ex(Exception e) {
        e.printStackTrace();
        return Result.error("服务器异常");
    }

    //截取异常出来的信息
    @ExceptionHandler
    public Result handDuplicateKeyException(DuplicateKeyException e) {
        log.info("程序出错了");
        String message = e.getMessage();
        int i = message.indexOf("Duplicate entry");
        String str = message.substring(i);
        String[] arr = str.split(" ");
        return Result.error(arr[2] + "重复");

    }



}
